import re
from collections.abc import Iterator
from dataclasses import dataclass


@dataclass(frozen=True, eq=True)
class AlertPosition:
    line: int
    column: int


@dataclass(frozen=True, eq=True)
class CompilerAlert:
    msg: bytes
    file: bytes
    position: AlertPosition

    @property
    def line(self) -> int:
        return self.position.line

    @property
    def column(self) -> int:
        return self.position.column


_ERROR_PATTERN = re.compile(rb'^\s*(?P<message>.*?) \[(?P<file>\S+):(?P<line>\d+):(?P<column>\d+)\]\s*$')


def parse_alerts(data: bytes) -> Iterator[CompilerAlert]:
    for m in _ERROR_PATTERN.finditer(data):
        g = m.groups()
        yield CompilerAlert(
            msg=bytes(g['message']),
            file=bytes(g['file']),
            position=AlertPosition(
                line=int(g['line']),
                column=int(g['column']),
            ),
        )
